<?php

// 使用用新浪云Storage
use sinacloud\sae\Storage as Storage;
// https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
// include_once();
require_once('request.php');
// 获取新的access_token
function getNewAccess_token(){
  $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx3babaf2c7f48c3d2&secret=198c5f5866b3f2fffcdc3400f5014354";
  $response = httpGet($url);
  // 解json串
  $responseObj = json_decode($response);
  // 获取access_token 和有效期
  $access_token = $responseObj->access_token;
  // 有效期
  $expires_in = $responseObj->expires_in;//7200
  // 当前时间 + 有效期 - 200 = 过期时间
  $expires_time = time() + $expires_in - 200;
  // 将过期时间和access_token转换成字符串写入storage中
  $saveObj = (object)array();//创建一个对象
  $saveObj->access_token = $access_token;
  $saveObj->$expires_time = $expires_time;
  // 转化成json串
  $saveStr = json_encode($saveObj);
  // 创建storage对象
  $storage = new Storage();
  // 获取bucket
  // @消除用户警告
  $bucket = @$storage->getBucket('bucket');//参数是bucket的名字
  if ($bucket == false) {//如果bucket为空
    // 创建一个bucket r:* 权限公开
    $bucket = $storage->putBucket("bucket","r:*");
  }
  // 往bucket中写入数据
  // 第一个参数 要写入的数据
  // 第二个参数 要写入那个bucket中
  // 第三个参数 要写入那个文件
  // 第四个参数 一个空数组
  // 第五个参数 存入数据的文件头
  $storage->putObject($saveStr,"bucket","access_token.txt",array(),array('Content-type'=>'text/plain;charset=utf-8'));
  return $access_token;
}
// 从storage中获取access_token
function getAccess_tokenFromStorage(){
  // 创建一个storage对象
  $storage = new Storage();
  // 获取bucket
  $bucket = @$storage->getBucket("bucket");
  if ($bucket == false) {
    // 如果没有bucket 则返回false
    return false;
  }
  // 如果有bucket
  // 先获取bucket中存储的内容  第一个参数是bucket名字  第二个参数是存储内容的文件名
  $object = $storage->getObject("bucket","access_token.txt");
  // 将存储的内容转成json串并返回
  return json_encode($object->body);
}
// 获取access_token
function getAccess_token(){
  // 先从storage中获取access_token
  $string = getAccess_tokenFromStorage();
  if ($string) {//有两种情况
    // 有$string  json串
    $response = json_decode($string);
    // 获取存储的对象
    $object = json_decode($response);
    // 获取存储的过期时间
    $expires_time = $object->expires_time;
    // 获取当前时间进行比较
    $time = time();

    if ($time > $expires_time) {

      //当前时间超过了过期时间  获取新的access_token
      $access_token = getNewAccess_token();
      return $access_token;
    }else{

      // 如果没有过期就直接返回object中的access_token
      return $object->access_token;
    }

  }else{//没有
    // 如果没有bucket  就直接获取新的access_token  并创建Bucket 存储access_token

      $access_token = getNewAccess_token();
    return $access_token;
  }
}
// 获取微信服务器ip
function getWXServerIp(){
  $access_token = getAccess_token();
  $url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$access_token;
  $response = httpGet($url);
  echo $response;
}
 // getWXServerIp();
 // 获取微信菜单
 $data = '{"button":[{
     "name":"蛋糕商城",
     "sub_button":[{
        "type":"view",
        "name":"选择蛋糕",
        "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3babaf2c7f48c3d2&redirect_uri=http://llllc.applinzi.com/b520/php/web_skip.php&response_type=code&scope=snsapi_userinfo&state=select#wechat_redirect"
     },{
       "type":"view",
       "name":"输入收货信息",
       "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3babaf2c7f48c3d2&redirect_uri=http://llllc.applinzi.com/b520/php/web_skip.php&response_type=code&scope=snsapi_userinfo&state=info#wechat_redirect"
     },{
       "type":"view",
       "name":"团购",
       "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3babaf2c7f48c3d2&redirect_uri=http://llllc.applinzi.com/b520/php/web_skip.php&response_type=code&scope=snsapi_userinfo&state=together#wechat_redirect"
     },{
       "type":"view",
       "name":"我的订单",
       "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3babaf2c7f48c3d2&redirect_uri=http://llllc.applinzi.com/b520/php/web_skip.php&response_type=code&scope=snsapi_userinfo&state=my#wechat_redirect"
     }]
   },{
     "name":"蛋糕DIY",
     "sub_button":[{
        "type":"view",
        "name":"乐趣分享",
        "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3babaf2c7f48c3d2&redirect_uri=http://llllc.applinzi.com/b520/php/web_skip.php&response_type=code&scope=snsapi_userinfo&state=share#wechat_redirect"
     },{
       "type":"view",
       "name":"我要参与",
       "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3babaf2c7f48c3d2&redirect_uri=http://llllc.applinzi.com/b520/php/web_skip.php&response_type=code&scope=snsapi_userinfo&state=join#wechat_redirect"
     }]
   },{
     "name":"关于我们",
     "sub_button":[{
        "type":"view",
        "name":"成为商家",
        "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3babaf2c7f48c3d2&redirect_uri=http://llllc.applinzi.com/b520/php/web_skip.php&response_type=code&scope=snsapi_userinfo&state=change#wechat_redirect"
     },{
       "type":"view",
       "name":"投诉建议",
       "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3babaf2c7f48c3d2&redirect_uri=http://llllc.applinzi.com/b520/php/web_skip.php&response_type=code&scope=snsapi_userinfo&state=suggest#wechat_redirect"
     },{
       "type":"view",
       "name":"在线客服",
       "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3babaf2c7f48c3d2&redirect_uri=http://llllc.applinzi.com/b520/php/web_skip.php&response_type=code&scope=snsapi_userinfo&state=services#wechat_redirect"
     },{
       "type":"view",
       "name":"了解我们",
       "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3babaf2c7f48c3d2&redirect_uri=http://llllc.applinzi.com/b520/php/web_skip.php&response_type=code&scope=snsapi_userinfo&state=know#wechat_redirect"
     }]
   }
   ]}';
  //  获取菜单
   function getMenu($data){
     $access_token = getAccess_token();
     $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
     $response = httpPost($url,$data);
     return $response;
   }
  echo getMenu($data);
  // 查询菜单
  function getMenuInfo(){
    $access_token = getAccess_token();
    $url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$access_token;
    $response = httpGet($url);
    return $response;
  }
  // 查询菜单
  echo getMenuInfo();
  // 自定义菜单删除接口
  function deletMenuPort(){
    $access_token = getAccess_token();
    $url = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$access_token;
    $response = httpGet($url);
    return $response;
  }

// 查询默认菜单和个性菜单
//echo getMenuInfo();
//$json = '{"menuid":411171091}';
// 删除个性化菜单
function deleteCustomMenu($json){
  $access_token = getAccess_token();
  $url = "https://api.weixin.qq.com/cgi-bin/menu/delconditional?access_token=".$access_token;
  $response = httpPost($url,$json);
  return $response;
}
//删除所有个性化菜单
function deleteAllCustomMenu(){
  $object = json_decode(getMenuInfo());
  //用数组接收
  $array = array();
  $array = $object->conditionalmenu;
  //创建一个数组  专门存放menuid
  $menuIdArray = array();
  //是一个对象  里面有三个属性  我们需要的是menuid
  for($i = 0;$i < count($array);$i++){
  	//获取每一个对象
    $object = $array[$i];
  	$menuIdArray[] = $object->menuid;
  }
  for($i = 0;$i < count($menuIdArray);$i++){
  	$menuid = $menuIdArray[$i];
    $json = '{"menuid":'.$menuid.'}';
    echo deleteCustomMenu($json);
  }
  echo getMenuInfo();
}
deleteAllCustomMenu();
//echo deleteCustomMenu($json);
?>
